﻿Public Class frmAcesso
    Private prvUsuario As Int32
    Private objUsuario As New Controle.ctrUsuario
    Private prvLogin As String
    Private prvSenha As String


    Public Property Usuario()
        Get
            Usuario = prvUsuario
        End Get
        Set(ByVal value)
            prvUsuario = value
        End Set
    End Property
    Private Function ValidarFormulario() As Boolean
        Dim bValidacao As Boolean
        Try
            bValidacao = True

            If (txtLogin.Text = String.Empty) Then
                MsgBox("Login não informado.", MsgBoxStyle.Exclamation, "Login")
                bValidacao = False
                txtLogin.Focus()
                Exit Function
            End If
            If (txtSenha.Text = String.Empty) Then
                MsgBox("Senha não informada.", MsgBoxStyle.Exclamation, "Login")
                bValidacao = False
                txtSenha.Focus()
                Exit Function
            End If

        Catch ex As Exception
            Throw New Exception("Exceção no método 'ValidarFormulario'. " & Environment.NewLine & Environment.NewLine & ex.Message)
            bValidacao = False
        End Try

        Return bValidacao

    End Function

    Private Sub cmdOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOk.Click
        Try
            If (ValidarFormulario()) Then
                prvLogin = txtLogin.Text.Trim
                prvSenha = txtSenha.Text.Trim
                prvUsuario = objUsuario.VerificaLogin(prvLogin, prvSenha)
                If (prvUsuario >= 0) Then
                    Me.DialogResult = Windows.Forms.DialogResult.OK
                    Me.Close()
                Else
                    txtLogin.Focus()
                End If
            End If
        Catch ex As Exception
            Throw New Exception("Exceção no método 'Login'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try
    End Sub

    Private Sub cmdSair_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSair.Click
        frmPrincipal.Close()
        Me.Close()
    End Sub

    Private Sub txtLogin_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtLogin.GotFocus
        txtLogin.SelectAll()
    End Sub

    Private Sub txtLogin_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtLogin.KeyPress

        If e.KeyChar = Convert.ToChar(13) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If

    End Sub

    Private Sub txtSenha_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSenha.KeyPress

        If e.KeyChar = Convert.ToChar(13) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If

    End Sub
End Class